const sqlite3 = require('sqlite3').verbose();
const path = require('path');

// 连接到SQLite数据库
const dbPath = path.join(__dirname, 'database.sqlite');
const db = new sqlite3.Database(dbPath, (err) => {
  if (err) {
    console.error('数据库连接失败:', err.message);
  } else {
    console.log('已连接到SQLite数据库');
    seedDatabase();
  }
});

// 填充示例数据
function seedDatabase() {
  // 插入示例货物数据
  const sampleGoods = [
    { name: '笔记本电脑', sku: 'Laptop-001', quantity: 50, location: 'A-01-01', status: 'inStock', batch_number: 'BATCH-2025-001' },
    { name: '无线鼠标', sku: 'Mouse-001', quantity: 200, location: 'A-01-02', status: 'inStock', batch_number: 'BATCH-2025-002' },
    { name: '机械键盘', sku: 'Keyboard-001', quantity: 100, location: 'A-01-03', status: 'inStock', batch_number: 'BATCH-2025-003' },
    { name: '显示器', sku: 'Monitor-001', quantity: 30, location: 'A-02-01', status: 'inStock', batch_number: 'BATCH-2025-004' },
    { name: '移动硬盘', sku: 'HDD-001', quantity: 75, location: 'A-02-02', status: 'inStock', batch_number: 'BATCH-2025-005' }
  ];

  let goodsInserted = 0;
  sampleGoods.forEach(good => {
    const insertQuery = `
      INSERT OR IGNORE INTO goods (name, sku, quantity, location, status, batch_number)
      VALUES (?, ?, ?, ?, ?, ?)
    `;
    db.run(insertQuery, [good.name, good.sku, good.quantity, good.location, good.status, good.batch_number], function(err) {
      if (err) {
        console.error('插入货物数据失败:', err.message);
      } else {
        goodsInserted++;
        console.log(`货物 "${good.name}" 已插入`);
      }
      
      // 检查是否所有货物都已插入
      if (goodsInserted === sampleGoods.length) {
        console.log(`总共插入了 ${goodsInserted} 个货物`);
        insertSampleDocuments();
      }
    });
  });
}

// 插入示例单据数据
function insertSampleDocuments() {
  // 插入示例入库单据
  const sampleInboundDocs = [
    { goods_id: 1, supplier: '联想集团', quantity: 10, expected_arrival_date: '2025-11-10', status: 'pending', operator: '张三' },
    { goods_id: 2, supplier: '罗技公司', quantity: 50, expected_arrival_date: '2025-11-12', status: 'pending', operator: '李四' },
    { goods_id: 3, supplier: '雷蛇公司', quantity: 30, expected_arrival_date: '2025-11-15', status: 'completed', operator: '王五' }
  ];

  let inboundInserted = 0;
  sampleInboundDocs.forEach(doc => {
    const insertQuery = `
      INSERT OR IGNORE INTO inbound_documents 
      (goods_id, supplier, quantity, expected_arrival_date, status, operator)
      VALUES (?, ?, ?, ?, ?, ?)
    `;
    db.run(insertQuery, [doc.goods_id, doc.supplier, doc.quantity, doc.expected_arrival_date, doc.status, doc.operator], function(err) {
      if (err) {
        console.error('插入入库单据数据失败:', err.message);
      } else {
        inboundInserted++;
        console.log(`入库单据 (供应商: ${doc.supplier}) 已插入`);
      }
      
      // 检查是否所有入库单据都已插入
      if (inboundInserted === sampleInboundDocs.length) {
        console.log(`总共插入了 ${inboundInserted} 个入库单据`);
      }
    });
  });

  // 插入示出示例出库单据
  const sampleOutboundDocs = [
    { goods_id: 1, customer: '北京科技公司', quantity: 5, expected_pickup_date: '2025-11-05', status: 'pending', operator: '赵六' },
    { goods_id: 4, customer: '上海贸易公司', quantity: 2, expected_pickup_date: '2025-11-08', status: 'picked', operator: '孙七' },
    { goods_id: 5, customer: '广州电子公司', quantity: 10, expected_pickup_date: '2025-11-10', status: 'completed', operator: '周八' }
  ];

  let outboundInserted = 0;
  sampleOutboundDocs.forEach(doc => {
    const insertQuery = `
      INSERT OR IGNORE INTO outbound_documents 
      (goods_id, customer, quantity, expected_pickup_date, status, operator)
      VALUES (?, ?, ?, ?, ?, ?)
    `;
    db.run(insertQuery, [doc.goods_id, doc.customer, doc.quantity, doc.expected_pickup_date, doc.status, doc.operator], function(err) {
      if (err) {
        console.error('插入出库单据数据失败:', err.message);
      } else {
        outboundInserted++;
        console.log(`出库单据 (客户: ${doc.customer}) 已插入`);
      }
      
      // 检查是否所有出库单据都已插入
      if (outboundInserted === sampleOutboundDocs.length) {
        console.log(`总共插入了 ${outboundInserted} 个出库单据`);
        closeDatabase();
      }
    });
  });
}

// 关闭数据库连接
function closeDatabase() {
  db.close((err) => {
    if (err) {
      console.error('关闭数据库连接失败:', err.message);
    } else {
      console.log('数据库连接已关闭');
      console.log('示例数据填充完成！');
    }
  });
}